package View;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;

import Controller.ComboSortAction;
import Controller.ContactController;
import Controller.ContactListAction;
import Controller.MainFrameActionListener;
import Controller.MenuBarActionListener;
import Controller.OnCloseEvent;
import Model.Contact;
import Model.Group;

/**
 * the main frame of the application
 * @author Vincent Francolin
 * @version 1.1 : menu bar + multi threading
 * 			1.0 : creation
 * http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
 */
public class MainFrame extends JFrame {
	private JPanel centralFrame;
	private JPanel leftenFrame;
	private JPanel upperFrame;
	private JMenuBar menuBar;
	
	private JButton add;
	private JButton modify;
	private JButton delete;
	private JButton createGroup;
	
	private JScrollPane contactList;
	private JList contentList;
	private DefaultListModel listData;
	
	private JComboBox comboSort;
	
	private ContactController contactController;
	
	/**
	 * default constructor
	 * @param controller the main controller of the application
	 */
	public MainFrame (ContactController controller){
		super();
		listData= new DefaultListModel();
		contentList = new JList(listData);
		contentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		contentList.setLayoutOrientation(JList.VERTICAL);
		contentList.setVisibleRowCount(-1);
		
		contentList.addListSelectionListener(new ContactListAction(contentList,controller,this) );
		
		add = new JButton("Add a contact");
		modify = new JButton("Modify a contact");
		delete = new JButton("Delete a contact");
		createGroup = new JButton("Create a group");
		
		add.addActionListener(new MainFrameActionListener(this,controller));
		modify.addActionListener(new MainFrameActionListener(this,controller));
		delete.addActionListener(new MainFrameActionListener(this,controller));
		createGroup.addActionListener(new MainFrameActionListener(this,controller));
		
		centralFrame = new JPanel();
		leftenFrame = new JPanel();
		upperFrame = new JPanel();
		
		contactList = new JScrollPane(contentList);
		contactList.setPreferredSize(new Dimension(200, 400));
		
		String[] comboContent = new String[3];
		comboContent[0] = "Creation date";
		comboContent[1] = "Alphanumeric order";
		comboContent[2] = "Group";
		
		comboSort = new JComboBox(comboContent);
		comboSort.addActionListener(new ComboSortAction(this,controller));
		
		//the function execute immediatly this.setDefaultCloseOperation(controller.saveContextBeforeExit());
		//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addWindowListener(new OnCloseEvent(controller));
		
		initView();
		
		initMenuBar(controller);
		this.setJMenuBar(menuBar);
	}
	
	/**
	 * initialize the view of the main frame
	 */
	private void initView(){
		this.setLayout(new GridBagLayout());
	    GridBagConstraints cons = new GridBagConstraints();
	    
		initLeftenFrame();
		initUpperFrame();
		initCentralFrame();
		
		cons.insets = new Insets(10,10,10,10); 
		
		cons.gridx = 0;
	    cons.gridy = 0;
	    cons.gridwidth = 1;
	    cons.gridheight = 6;
	    add(leftenFrame,cons);
	    
	    cons.gridx = 1;
	    cons.gridy = 0;
	    cons.gridwidth = 3 ;
	    cons.gridheight = 1;
	    add(upperFrame,cons);
	    
	    cons.gridx = 1;
	    cons.gridy = 1;
	    cons.gridwidth = 3;
	    cons.gridheight = 5;
	    add(centralFrame,cons);
	}
	
	/**
	 * initialize the left part of the frame where there is the contact list
	 */
	private void initLeftenFrame(){
		 leftenFrame.setLayout(new GridBagLayout());
	     GridBagConstraints cons = new GridBagConstraints();
	     
	     cons.insets = new Insets(5,5,5,5);
	     
	     cons.gridx = 0;
	     cons.gridy = 0;
	     cons.gridwidth = 2;
	     leftenFrame.add(new JLabel("List of contacts"),cons);
	     
	     cons.gridwidth = 1;
	     cons.gridy = 1;
	     cons.gridx = 0;   
	     leftenFrame.add(new JLabel("Order by : "),cons);
	     cons.gridx = 1;
	     leftenFrame.add(comboSort,cons);
	     
	     cons.gridy = 2;
	     cons.gridx = 0;
	     cons.gridwidth = 2;
	     leftenFrame.add(contactList,cons);
	}
	
	/**
	 * initialize the upper frame where there is the buttons for user control
	 */
	private void initUpperFrame(){
		 upperFrame.setLayout(new GridBagLayout());
	     GridBagConstraints cons = new GridBagConstraints();
	     cons.insets = new Insets(5,5,5,5); 
	     cons.gridx = 0;
	     cons.gridy = 0;
	     upperFrame.add(add,cons);
	     cons.gridx = 1;
	     upperFrame.add(modify,cons);
	     cons.gridx = 2;
	     upperFrame.add(delete,cons);
	     cons.gridx = 3;
	     upperFrame.add(createGroup,cons);
	}
	
	/**
	 * initialize the central frame where there is contact info or form
	 */
	public void initCentralFrame(){
		centralFrame.add(new JLabel("Select a contact to display its information"));
	}
	
	public void initMenuBar(ContactController controller){
		menuBar = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenu contacts = new JMenu("Contacts");
		//JMenu groups = new JMenu("Groups");
		//JMenu help = new JMenu("Help");
		
		/*JMenuItem importProfile = new JMenuItem("Import a profile...");
		importProfile.addActionListener(new MenuBarActionListener(controller));
		JMenuItem exportProfile = new JMenuItem("Export a profile...");
		exportProfile.addActionListener(new MenuBarActionListener(controller));*/
		JMenuItem reset = new JMenuItem("Reset all");
		reset.addActionListener(new MenuBarActionListener(controller));
		JMenuItem quit = new JMenuItem("Quit");
		quit.addActionListener(new MenuBarActionListener(controller));
		
		JMenuItem importContact = new JMenuItem("Import a contact...");
		importContact.addActionListener(new MenuBarActionListener(controller));
		JMenuItem exportContact = new JMenuItem("Export a contact...");
		exportContact.addActionListener(new MenuBarActionListener(controller));
		JMenuItem deleteContact = new JMenuItem("Delete contacts...");
		deleteContact.addActionListener(new MenuBarActionListener(controller));
		
		/*JMenuItem createGroup = new JMenuItem("Create a goup...");
		createGroup.addActionListener(new MenuBarActionListener(controller));
		JMenuItem deleteGroups = new JMenuItem("Delete groups...");
		deleteGroups.addActionListener(new MenuBarActionListener(controller));*/
		
		//file.add(importProfile);
		//file.add(exportProfile);
		file.add(reset);
		file.add(quit);
		
		contacts.add(importContact);
		contacts.add(exportContact);
		contacts.add(deleteContact);
		
		/*groups.add(createGroup);
		groups.add(deleteGroups);*/
		
		menuBar.add(file);
		menuBar.add(contacts);
		//menuBar.add(groups);
		//menuBar.add(help);
	}
	
	/**
	 * change the central frame to the default view
	 */
	public void changeDefaultView(){
		centralFrame.setVisible(false);
		centralFrame.removeAll();
		centralFrame.add(new JLabel("Select a contact to display its information"));
		centralFrame.setVisible(true);
		pack();
	}
	
	/**
	 * change the central frame to display contact info
	 * @param c the contact data to display
	 */
	public void changeContactView(Contact c){
		centralFrame.setVisible(false);
		centralFrame.removeAll();
		centralFrame.add(new JLabel("Loading in progress..."));
		centralFrame.setVisible(true);
		final Contact data = c;
		(new SwingWorker(){
			ContactView toAdd;
			@Override
			protected Object doInBackground() throws Exception {
				if(data!=null)
					toAdd = new ContactView(data);
				return null;
			}
			
			@Override
		       protected void done() {
		           try { 
		        	   centralFrame.setVisible(false);
			       		centralFrame.removeAll();
			       		if(data!=null)
			       			centralFrame.add(toAdd);
			       		else
			       			centralFrame.add(new JLabel("Select a contact to display its information"));
			       		centralFrame.setVisible(true);
		           } catch (Exception ignore) {
		           }
		       }
		}).execute();
		
		pack();
	}
	
	/**
	 * change the central frame to display form to create or modify a contact
	 * @param c the contact in case of modification null otherwise
	 * @param g the group the contact belongs to, null if no contact
	 * @param controller main controller of the application
	 */
	public void changeFormView(Contact c,Group g,ContactController controller){
		centralFrame.setVisible(false);
		centralFrame.removeAll();
		centralFrame.add(new FormView(c,g,controller));
		centralFrame.setVisible(true);
		pack();
	}
	
	/**
	 * add string for a contact in the contact list
	 * @param str string representing the contact
	 */
	public void addContactToView(String str){
		listData.addElement(str);
		contentList = new JList(listData);
		System.out.println("add in the list");
	}
	
	/**
	 * remove a contact from the contact list
	 * @param id if of the contact
	 */
	public void removeContactFromView(int id){
		listData.removeElementAt(id);
		contentList = new JList(listData);
		System.out.println("remove in the list");
	}
	
	/**
	 * update the string in the contact list in case of modification in contact info
	 * @param string the new string to put
	 * @param id the id of the contact
	 */
	public void updateContactInList(String string,int id) {
		// TODO Auto-generated method stub
		listData.set(id, string);
		contentList = new JList(listData);
	}
	
	public void updateEntireList(String[] input){
		listData.clear();
		if(input!=null){
			for(String obj : input){
				listData.addElement(obj);
			}
		}
		contentList = new JList(listData);
	}
}
